﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Input;
using VolunteerManager.Model;

namespace VolunteerManager.ViewModel
{

    public class EditViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand changePhotoCommand;
        private Voluteer _vol;

        private Voluteer _volClone;


        public Voluteer CurrentVoluteer
        {
            get
            {
                return this._vol;
            }
            set
            {
                this._vol = value;
                this._volClone = value.Clone();

                if (value.ID > 0)
                {
                    List<Group> GroupDataSource = ServiceLocator.Current.GetInstance<CommonDataSource>().GroupDataSource;

                    var resultList = from item in GroupDataSource
                                     where item.ID == _vol.GroupID
                                     select item;

                    var escortList = resultList.ToList();
                    if (escortList.Count > 0)
                    {
                        this._vol.Group = escortList[0];
                    }
                    else
                    {
                        this._vol.Group = GroupDataSource[0];
                    }

                }
                this.RaisePropertyChanged("CurrentVoluteer");
            }
        }

        public void save()
        {

            if (CurrentVoluteer.Photo.LastIndexOf("\\") > 0)    //表示选择 照片，复制照片到当前目录
            {
                string newName =  CurrentVoluteer.ID + ".bmp";
                System.IO.File.Copy(this.CurrentVoluteer.Photo, Config.PhotoRoot + newName, true);
                CurrentVoluteer.Photo = newName;
            }
            this.CurrentVoluteer.Save();
            this.CurrentVoluteer.DoRaisePropertyChanged();
          
            Messenger.Default.Send<object>(null, "CloseEditWindow");
        }
        private void cancel()
        {
            this._vol.RollBack(this._volClone);
            Messenger.Default.Send<object>(null, "CloseEditWindow");
        }
        public ICommand SaveCommand
        {
            get
            {
                if (null == saveCommand)
                {
                    saveCommand = new RelayCommand(() => save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (null == cancelCommand)
                {
                    cancelCommand = new RelayCommand(() => cancel());
                }
                return cancelCommand;
            }
        }
        
        public ICommand ChangePhotoCommand
        {
            get
            {
                if (null == changePhotoCommand)
                {
                    changePhotoCommand = new RelayCommand(() => changePhoeo());
                }
                return changePhotoCommand;
            }
        }

        private void changePhoeo()
        {
            Microsoft.Win32.OpenFileDialog od = new Microsoft.Win32.OpenFileDialog();
            od.Filter = "(*.jpg,,*.bmp,*.png,*.jpeg)|*.JPG;*.BMP;*.PNG;*.JPEG";
            if (od.ShowDialog().Value)
            {
                this.CurrentVoluteer.Photo = od.FileName;
                this.CurrentVoluteer.RaisePropertyChanged("Photo");
            }
        }

        public EditViewModel(IDataService dataService)
        {
            _dataService = dataService;


            if (IsInDesignMode)
            {
                this.CurrentVoluteer = dataService.Vol;
            }
            else {
                this.CurrentVoluteer = new Voluteer();
            }
        }
    }
}
